home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / Yenta / Periodic Tasksƒ / CPPConfirmUsers.cp next >
Encoding:
Text File  |  1996-04-04  |  4.3 KB  |  160 lines  |  [TEXT/KAHL]

  1. /********************************************************* DEFINITION
  2.     DATE:    9/17/93
  3.     AUTHOR: Eric R. Rosé
  4.     
  5.     CLASS:  CPPConfirmUsers
  6.     
  7.     SUPERCLASS: CPPPeriodicTask
  8.     
  9.         This C++ class goes through our list of nodes, checking to
  10.         make sure that they are all still on the network.  If 
  11.         they are not, it deletes them from the list.
  12.     
  13. ********************************************************************/
  14.  
  15. #include <CPPTaskManager.h>
  16. #include <CPPNodeInfo.h>
  17. #include <CPPObjectList.h>
  18. #include <CPPConfirmTask.h>
  19. #include "CPPUserList.h"
  20. #include "CPPConfirmUsers.h"
  21. #include <CPPSound.h>
  22. #include "MyGlobals.h"
  23. #include <MemoryTools.h>
  24. #include <StringTools.h>
  25.  
  26. extern    CPPUserList    *gUserList;
  27. extern    CPPSound    *gLogonSound;
  28. extern    PrefsData    gPrefsInfo;
  29.  
  30.         void    ConfirmCompletionProc (CPPObject *TheTask);
  31. extern    void    SetStatusMessage (StringPtr NewMessage, Boolean MakeCopy);
  32. extern        StringPtr    ShortName     (CPPNodeInfo *theNode);
  33.  
  34.  
  35. /*-----------------------------------------------------------------*/
  36. /*------------------------ PUBLIC METHODS -------------------------*/
  37. /*-----------------------------------------------------------------*/
  38.  
  39.     CPPConfirmUsers::CPPConfirmUsers (CPPTaskManager *TaskManager, 
  40.                                      long minPeriod, 
  41.                                      Boolean deleteWhenDone) :
  42.                                        CPPPeriodicTask (TaskManager, minPeriod,
  43.                                                      deleteWhenDone)
  44.     {
  45.         nodeList = NULL;
  46.         confirmTask = NULL;
  47.         whichNode = 0;
  48.         this->confirmTask = NULL;
  49.     }
  50.     
  51. /*-----------------------------------------------------------------*/
  52.  
  53.     CPPConfirmUsers::~CPPConfirmUsers (void)
  54.     {
  55.         if (confirmTask)
  56.           delete confirmTask;
  57.     }
  58.     
  59. /*-----------------------------------------------------------------*/
  60.  
  61.     char *CPPConfirmUsers::ClassName (void)
  62.     {
  63.         return "CPPConfirmUsers";
  64.     }
  65.  
  66. /*-----------------------------------------------------------------*/
  67.  
  68.     void    ConfirmCompletionProc (CPPObject *TheTask)
  69.     /* This routine will be called when a 'confirm' task completes;*/
  70.     /* It's job is to delete the node from the list if the confirm */
  71.     /* task returns 'not here' */
  72.     {
  73.         CPPConfirmTask    *CTask = (CPPConfirmTask *)TheTask;
  74.         Str255        STemp;
  75.         StringPtr    UsersName;
  76.         Boolean        done = FALSE;
  77.         
  78.             // get the name of the connection we are confirming
  79.             UsersName = ShortName (CTask->NodeToConfirm);
  80.                     
  81.         if (!CTask->NodeExists(&done))
  82.           {
  83.             gUserList->DeleteUser(CTask->NodeToConfirm);
  84.             if (gPrefsInfo.playLogon)
  85.               gLogonSound->PlaySound(TRUE);
  86.             PStrCat (255, STemp, 2, "\pLost connection with ", UsersName);
  87.             SetStatusMessage (STemp, TRUE);
  88.           }
  89.  
  90.         NukePtr(UsersName);
  91.     }
  92.  
  93. /*-----------------------------------------------------------------*/
  94.  
  95.     void    CPPConfirmUsers::DoPeriodicAction (void)
  96.     /* if the confirm has completed, advance to the next node and */
  97.     /* start another confirm */
  98.     {
  99.         CPPNodeInfo    *TheNode = NULL;
  100.         Str255        STemp;
  101.         StringPtr    UsersName;
  102.         
  103.         // call the inherited method to update frequency count
  104.         CPPPeriodicTask::DoPeriodicAction();
  105.  
  106.         if (confirmTask->hasCompleted)
  107.           {
  108.               this->whichNode++;
  109.               if (this->whichNode <= gUserList->GetNumItems())
  110.                 {
  111.                     TheNode = (CPPNodeInfo *)((*gUserList)[whichNode]);
  112.                     
  113.                     // tell the user we are confirming this connection
  114.                     UsersName = ShortName (TheNode);
  115.                 PStrCat (255, STemp, 2, "\pSearching for ", UsersName);
  116.                     SetStatusMessage (STemp, TRUE);
  117.                     NukePtr(UsersName);
  118.                     
  119.                     // do the confirmation
  120.                 confirmTask->StartNodeConfirm(TheNode, ConfirmCompletionProc);
  121.                 }
  122.               else
  123.                 {
  124.                     this->hasCompleted = TRUE;
  125.                     this->callResult = noErr;
  126.                 }
  127.           }
  128.     }
  129.  
  130.  
  131. /*-----------------------------------------------------------------*/
  132.  
  133.     void    CPPConfirmUsers::DoCompletedAction (void)
  134.     {
  135.         CPPPeriodicTask::DoCompletedAction();
  136.     }
  137.  
  138. /*-----------------------------------------------------------------*/
  139.  
  140.     void CPPConfirmUsers::StartConfirmUsers (CompletionProc DoProc)
  141.     {
  142.         if (!this->hasCompleted)
  143.           return;
  144.  
  145.         // get rid of the old confirm task (if any)
  146.         if (confirmTask)
  147.           delete confirmTask;
  148.           
  149.         // set up the confirm task
  150.         this->confirmTask = new CPPConfirmTask (this->ourManager, 120, FALSE);
  151.     
  152.         // note that we haven't completed yet
  153.         SetCompletionProc(DoProc);
  154.         this->hasCompleted = FALSE;
  155.         this->whichNode = 0;
  156.         
  157.         // add ourselves to the periodic task queue
  158.         this->ourManager->AddPeriodicTask(this);
  159.     }
  160.